/**
 * 
 */
package cn.com.xf.utils.encode.impl;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.xf.utils.encode.EncodeService;

/**
 * @author Administrator
 * 
 */
public class EncodeServiceSHAImpl implements EncodeService {

	private static Log logger = LogFactory.getLog(EncodeServiceSHAImpl.class);

	@Override
	public String decode2Str(String input) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int decode2Int(String input) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public long decode2Long(String input) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String encode(String input) {
		String result = "";
		try {
			result = shaEncode(input.getBytes("utf8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public String encode(int input) {
		return null;
	}

	@Override
	public String encode(long input) {
		// TODO Auto-generated method stub
		return null;
	}

	private String shaEncode(byte[] input) {
		try {
			MessageDigest md5 = MessageDigest.getInstance("SHA");
			md5.update(input);
			byte[] result = md5.digest();
			StringBuilder ret = new StringBuilder(result.length << 1);
			for (int i = 0; i < result.length; i++) {
				ret.append(Character.forDigit((result[i] >> 4) & 0xf, 16));
				ret.append(Character.forDigit(result[i] & 0xf, 16));
			}
			return ret.toString();
		} catch (NoSuchAlgorithmException e) {
			logger.error("没有加密算法..........:" + e.getMessage());
		}
		return null;
	}
}
